package com.cyz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyz.dao.DoctorDao;
import com.cyz.dao.HealthDao;
import com.cyz.dao.OldDao;
import com.cyz.dao.UserDao;
import com.cyz.entity.Doctor;
import com.cyz.entity.Health;
import com.cyz.entity.Old;
import com.cyz.entity.User;
import com.cyz.vo.HealthDTO;
import com.cyz.vo.OldImgDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/doctor/doctor")
public class PersonController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DoctorDao doctorDao;

    @Autowired
    private OldDao oldDao;

    @Autowired
    private HealthDao healthDao;

    @PostMapping("/getDocByAccount/{account}")
    public Doctor getDocByAccount(@PathVariable String account){
        User user = userDao.selectOne(new QueryWrapper<User>().eq("account", account));

        String name = user.getName();

        return doctorDao.selectOne(new QueryWrapper<Doctor>().eq("name", name));
    }

    @PostMapping("/updateDoc")
    public String updateDoc(Doctor doctor){
        System.out.println(doctor.getId());
        System.out.println(doctor.getName());
        int update = doctorDao.update(doctor, new QueryWrapper<Doctor>().eq("id", doctor.getId()));
        return update == 0 ? "更新失败" : "更新成功";
    }

    @PostMapping("/queryOld")
    public List<OldImgDTO> queryOld(){
        List<Old> olds = oldDao.selectList(new QueryWrapper<Old>());
        List<OldImgDTO> collect = olds.stream().map(item -> {
            OldImgDTO oldImgDTO = new OldImgDTO();
            String url = null;
            // Convert byte array to Base64 encoded string
            String base64 = Base64.getEncoder().encodeToString(item.getImg());

            // Construct the URL for the img tag with the data URI scheme
            url = "data:image/jpeg;base64," + base64;
            BeanUtils.copyProperties(item, oldImgDTO);
            oldImgDTO.setUrl(url);
            return oldImgDTO;
        }).collect(Collectors.toList());
        return collect;
    }

    @PostMapping("/getOldId")
    public List<Integer> getOldId(){
        return oldDao.selectList(new QueryWrapper<Old>()).stream().map(item -> item.getId()).collect(Collectors.toList());
    }

    @PostMapping("/getOldById/{id}")
    public Old getOldById(@PathVariable int id){
        return oldDao.selectById(id);
    }

    @PostMapping("/queryOldByCon")
    public List<OldImgDTO> queryOldByCon(String id, String name, String job){
        QueryWrapper<Old> wrapper = new QueryWrapper<>();
        if (!id.equals("")){
            int i = Integer.parseInt(id);
            if (i > 0) {
                wrapper.eq("id",i);
            }
        }
        if (name != null && !name.equals("")){
            wrapper.eq("name", name);
        }
        if (job != null && !job.equals("")){
            wrapper.eq("tel", job);
        }
        List<Old> Olds = oldDao.selectList(wrapper);
        List<OldImgDTO> collect = Olds.stream().map(item -> {
            OldImgDTO oldImgDTO = new OldImgDTO();
            BeanUtils.copyProperties(item, oldImgDTO);
            String base64 = Base64.getEncoder().encodeToString(item.getImg());

            // Construct the URL for the img tag with the data URI scheme
            String url = "data:image/jpeg;base64," + base64;

            oldImgDTO.setUrl(url);
            return oldImgDTO;
        }).collect(Collectors.toList());

        return collect;
    }

    @PostMapping("/queryHealthByCon")
    public List<HealthDTO> queryHealthByCon(String id, String name){
        QueryWrapper<Health> wrapper = new QueryWrapper<>();
        if (!id.equals("")){
            int i = Integer.parseInt(id);
            if (i > 0) {
                wrapper.eq("id",i);
            }
        }
        if (name != null && !name.equals("")){
            wrapper.eq("name", name);
        }
        List<Health> healths = healthDao.selectList(wrapper);
        List<HealthDTO> collect = healths.stream().map(item -> {
            HealthDTO healthDTO = new HealthDTO();
            BeanUtils.copyProperties(item, healthDTO);

            Old old = oldDao.selectById(item.getId());
            healthDTO.setHealth(old.getHealth());

            return healthDTO;
        }).collect(Collectors.toList());

        return collect;
    }
}
